layui.use(['form', 'layer', 'jquery', 'formSelects'], function () {
    var form = layui.form,
        layer = layui.layer,
        formSelects = layui.formSelects,
        $ = layui.jquery;

    //加载下拉框
    $.ajax({
        type: "get",
        url: ctx + "/dept/deptSelected",
        data: {},
        success: function (data) {
            var opt;
            if (data != null) {
                for (var i = 0; i < data.length; i++) {
                    opt = "<option value='" + data[i].id + "' selected >" + data[i].dept_name + "</option>";
                    $("#deptId").append(opt);
                }
                layui.form.render("select")
            }
        }
    });


    form.on('submit(addEmp)', function (data) {
        var index = top.layer.msg("数据提交中", {
            icon: 16,
            time: true,
            shade: 0.8
        })

        var formData = data.field;
        var url = ctx + "/emp/addEmp";

        $.post(url, formData, function (result) {
             if (result.code == 200) {
                top.layer.close(index);
                layer.msg(result.msg)
                $("#addEmp")[0].reset();

            } else {
                layer.close(index);
                $("#addEmp")[0].reset();

                layer.msg(result.msg, {time: 2000});
            }
        })
        return false;
    })


    // 加载角色下拉框  多选
    formSelects.config("selectId", {
        type: "post",
        searchUrl: ctx + "/role/queryAllRoles",
        keyName: "roleName",
        keyVal: "id"
    }, true)


})